function showmess(mess){
    let div = $("<div class=mymess>"+mess+"</div>");
    $("body").append(div);
    
    var intv = setInterval(()=>{
        let top = div.css("top");
        top = parseInt( top.split("px")[0]  );
        if( top > 500){
            div.remove();
            clearInterval(intv);
        }
        div.css("top", (top+5)+"px"  );
        div.css("opacity",  1-(top/(800.0))  );
    } ,17);
}

const app=new Vue({
    el: "#star_list",
    data(){
        return {
            father_list: [],  // 列表，里面有son
            mess:"",
            new_son: null //新增的时候用来临时
        }
    },
    methods: {
        openUrl:function(url){
            window.open(url,"_blank");
        },
        changeSonFather: function(){
            let ckbs = document.getElementsByName("ckbson");
            let selects = [];
            for(let i=0; i<ckbs.length; i++){
                if(ckbs[i].checked){
                    selects.push( parseInt( ckbs[i].value ));
                }
            }
            if(selects.length ==0) {
                alert("未选中网站");
                return;
            }
            let radios = document.getElementsByName("select_father");
            for(let i=0; i<radios.length; i++){
                if(radios[i].checked){
                    let fid = radios[i].value;
                    axios({
                        url: "changeSonFather",
                        method: 'post',
                        data: JSON.stringify({
                            sons_id: selects,
                            fid: fid
                        }),
                        headers: {
                            'Content-Type': "application/json"
                        }
                    }).then(res=>{
                        if(res.data.code == 20){
                            this.getAllFather();
                        }else{
                            console.log(res);
                            alert( res.data.mess );
                        }
                    }).catch(err=>{
                        console.log(err);
                        alert("出错");
                    });
                    return;
                }
            }
            alert("未选中目录");
        },
        deleteFather: function(fid){
            if( ! confirm("确认删除?") ){
                return;
            }
            axios({
                url: "deleteFather/" + fid
            }).then(res=>{
                if(res.data.code == 20){
                    this.getAllFather();
                }else{
                    alert(res.data.mess);
                }
            }).catch(err=>{
                    console.log(err);
                    alert("网络错误");
            });
        },
        addFather: function(){
            let name = prompt("目录名称");
            if(name == null || name == '') return;
            axios({
                url: "addFather/" + name
            }).then(res=>{
                if(res.data.code == 20){
                    this.getAllFather();
                }else{
                    alert("网络错误");
                }
            }).catch(err=>{
                console.log(err);
                alert("网络错误");
            });
        },
        deleteSon: function(sid){
            if( ! confirm("确认删除?") ){
                return;
            }
            axios({
                url: "deleteSon/" + sid
            }).then(res=>{
                if(res.data.code == 20){
                    this.getAllFather();
                }else{
                    alert(res.data.mess);
                }
            }).catch(err=>{
                    console.log(err);
                    alert("网络错误");
            });
        },
        changeFather: function(fid){
            let newName = prompt("输入新名字");
            axios({
                url: "updateFather",
                method: 'post',
                data: JSON.stringify({
                    id: fid,
                    name: newName
                }),
                headers: {
                    'Content-Type' :"application/json"
                }
            }).then(res=>{
                if(res.data.code == 20){
                    for(let i=0; i<this.father_list.length; i++){
                        let father = this.father_list[i];
                        if(father.id == fid){
                            father.name = newName;
                            break;
                        }
                    }
                }else{
                    alert(res.data.mess);
                }
            }).catch(err=>{
                    console.log(err);
                    alert("网络错误");
            });
        },
        changeSon: function(fid, son_id){
            let tmpSon=null;
            for(let i=0; i<this.father_list.length; i++){
                let father = this.father_list[i];
                if(father.id == fid){
                    for(let j=0; j<father.sons.length; j++){
                        let son = father.sons[j];
                        if(son.id == son_id){
                            tmpSon = son;
                            break;
                        }
                    }
                    break;
                }
            }
            if(tmpSon == null){
                return;
            }
            axios({
                url: "updateSon",
                method: 'post',
                data: JSON.stringify(tmpSon),
                headers: {
                    'Content-Type' :"application/json"
                }
            }).then(res=>{
                if(res.data.code == 20){
                    this.getAllFather();
                }else{
                    alert(res.data.mess);
                }
            }).catch(err=>{
                console.log(err);
                alert("网络错误");
            });
        },
        getAllFather: function(){
            axios({
                url: "getAllFather"
            }).then(res=>{
                if(res.data.code == 20){
                    this.father_list = res.data.data.arr;
                }else{
                    alert(res.data.mess);
                }
            }).catch(err=>{
                console.log(err);
                alert("网络错误");
            });
        },
        addSon: function(fid){
            let name = prompt("输入名称", "");
            let url = prompt("输入URL", "");
            if(name=='' && url==''){
                return;
            }
            axios({
                url: "addSon",
                method: "post",
                data: JSON.stringify({
                    id: 0,
                    name: name,
                    url: url,
                    folder_id: fid
                }),
                headers: {
                    'Content-Type' :"application/json"
                }
            }).then(res=>{
                if(res.data.code == 20){
                    this.getAllFather();
                }else{
                    alert(res.data.mess);
                }
            }).catch(err=>{
                console.log(err);
                alert("网络错误");
            });
        }
        
    },
    created(){
        this.getAllFather();
    }
});